package com.example.mapper.impl;

import com.example.mapper.UserMapper;
<<<<<<< HEAD
import com.example.dao.User;
=======
import com.example.pojo.User;
>>>>>>> 4a415ad7e6346b1b1956b12c9156b54f8097bc61
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserMapperImpl implements UserMapper {
    @Override
    public List<User> selectAll() {
        SqlSession sqlSession = null;
        InputStream is = null;
        List<User> list = null;

        //加载核心配置文件
        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true);
            //执行sql文件中的映射语句
            list = sqlSession.selectList("UserMapper.selectAll");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return list;
    }
<<<<<<< HEAD

    @Override
    public User selectId(Integer id) {
        {
            SqlSession sqlSession = null;
            InputStream is = null;
            List<User> list = null;
            User user = new User();

            //加载核心配置文件
            try {
                //加载核心配置文件
                is = Resources.getResourceAsStream("MybatisConfig.xml");
                //获取sqlsession工厂
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

                //获取sqlsession
                sqlSession = sqlSessionFactory.openSession(true);
                //执行sql文件中的映射语句
                 user= sqlSession.selectOne("UserMapper.selectId",id);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                //释放资源
                if(sqlSession != null){
                    sqlSession.close();
                }
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            return user;
        }
    }

    @Override
    public Integer insertId(User user) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Integer res = null;
        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true);
            //执行sql文件中的映射语句
            res = sqlSession.insert("UserMapper.insertUser", user);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return res;
    }

    @Override
    public Integer updateUser(User user) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Integer res = null;
        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true);
            //执行sql文件中的映射语句
            res  = sqlSession.insert("UserMapper.updateUser", user);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return res;
    }

    @Override
    public Integer deleteUserId(Integer id) {
        SqlSession sqlSession=null;
        InputStream is=null;
        Integer res=null;
        try {
            is=Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            res = sqlSession.delete("UserMapper.deleteUser", id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return res;
    }

=======
>>>>>>> 4a415ad7e6346b1b1956b12c9156b54f8097bc61
}